var store_score = ""; 	// store_score,药店得分
var log_score = "";     //log_score,物流得分 
var courierText, uidText; //复制订单号和快递号
var uid = sessionStorage.getItem("u_group_no");
$(function () {
	prescription_id = getQst("prescription_id").trim();
	
	/* 初始化订单信息 */
	var paramJson = {"actName": "GetIndentD","order_id":prescription_id, "utype": "5", "uid": uid, "rand": Math.random() };
	ajaxGet("http://zz.nxk520.com/nethosp/service/nethosp_action.ashx", paramJson, doOk);
	
	/* 配药发货的按钮点击事件 */
	$("#shop_order_details").on("click", ".medicine-btn", function(){
		var indent_no = $(this).attr("data-Iid");
		var type = $(this).attr("data-type"); //2为快递 1为自取
		var state = $(this).attr("data-state");
		if(type === "2" && state === "待发货"){
			window.location.href = "shop_courier.html?prescription_id=" + prescription_id + "&indent_no=" + indent_no
		}else if(state === "已取药" && type === "1"){
			var paramJsonEvaluation = {"actName": "DoReceiv", "order_id": prescription_id, "utype": "5", "uid": uid, "rows": "", "page": "", "rand": Math.random()};
			ajaxPost("http://zz.nxk520.com/nethosp/service/nethosp_action.ashx", paramJsonEvaluation, doOkComplete);
			/* 参数："actName": "DoReceiv","order_id":"订单号"
			返回：code 1成功 0失败，msg 失败原因 */
		}
		else{
			window.location.href = "shop_oneself.html?prescription_id=" + prescription_id + "&indent_no=" + indent_no
		}
		/* window.location.href = "" */
	});
	
	//药店
	for(var i = 0; i < parseInt(store_score); i++){
		$("#shop_evaluation_details").find("#service a").eq(i).addClass('on');
	}
	//物流
	for(var i = 0; i < parseInt(log_score); i++){
		$("#shop_evaluation_details").find("#courier a").eq(i).addClass('on');
	}
	
	/* 复制 */
	$("#shop_order_details").on("click", ".courier-btn", function(){
		var content = $(".courier-text").val();
		var message = null;
		console.log(content);
		var aux = document.createElement("input");
		aux.setAttribute("value", content); 
		document.body.appendChild(aux); 
		aux.select();
		document.execCommand("copy"); 
		document.body.removeChild(aux);
		if (message == null) {
		    alert("复制成功");
		} else{
		    alert(message);
		}
	});
	 
	$("#shop_order_details").on("click", ".uid-btn", function(){
		var content = $(".uid-text").val();
		var message = null;
		console.log(content);
		var aux = document.createElement("input");
		aux.setAttribute("value", content); 
		document.body.appendChild(aux); 
		aux.select();
		document.execCommand("copy"); 
		document.body.removeChild(aux);
		if (message == null) {
		    alert("复制成功");
		} else{
		    alert(message);
		}
	});
});

//获取数据
function doOk(rstData) {
	if (rstData.list.length > 0) {
		for (var i = 0; i < rstData.list.length; i++) {
			if(rstData.list[0].indent_state === "已完成"){
				/* 获取药店订单评价 */
				var paramJsonEvaluation = {"actName": "GetComment", "order_id": prescription_id, "utype": "5", "uid": uid, "rows": "", "page": "", "rand": Math.random()};
				ajaxGet("http://zz.nxk520.com/nethosp/service/nethosp_action.ashx", paramJsonEvaluation, doOkEvaluation);
				courierText = rstData.list[0].logistics_no;
				uidText = rstData.list[0].indent_no;
			}
		}
		//生成html片段
		var html = template("shopOrderDetails",rstData);
		var container = document.getElementById("shop_order_details");
		//将html片段渲染到界面中
		container.innerHTML = html;
	}else{
		//生成html片段
		var html = template("noData",rstData);
		var container = document.getElementById("shop_order_details");
		//将html片段渲染到界面中
		container.innerHTML = html;
	}
}

/*  */
function doOkComplete(res){
	if(res.code === 1){
		$("#div_EvaluationList").css("display" , "none");
	}
}

/* 订单评价 */
function doOkEvaluation(rstData) {
	if (rstData.list.length > 0) {
		//生成html片段
		var html = template("shopEvaluationDetails",rstData);
		var container = document.getElementById("shop_evaluation_details");
		//将html片段渲染到界面中
		container.innerHTML = html;
		for (var i = 0; i < rstData.list.length; i++) {
			store_score = rstData.list[0].store_score;
			log_score = rstData.list[0].log_score;
		}
	}else{
		//生成html片段
		var html = template("noData",rstData);
		var container = document.getElementById("shop_evaluation_details");
		//将html片段渲染到界面中
		container.innerHTML = html;
	}
}



